#import "YQPUserAttentionModel.h"

#import "MYMySetAttentionVC.h"

@interface MYMySetAttentionVC ()

@property (strong, nonatomic) NSMutableArray *dateArray;

@property (strong, nonatomic) YQPUserAttentionModel *listModel;

@end

@implementation MYMySetAttentionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    if (self.type == YQPUserFocus) {
        self.navigationItem.title = [NSString stringWithFormat:@"%@(%@)",kYQLS(@"我的关注"),self.model.loveNumber];
    }else{
        self.navigationItem.title = [NSString stringWithFormat:@"%@(%@)",kYQLS(@"我的粉丝"),self.model.fansNumber];
    }

    self.dateArray = NSMutableArray.new;
    self.page = 1;

    [self getUserRelationsCollectionsApi:YES];
    
    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserRelationsCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.dateArray.count < 10) {
            [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        self.page ++;

        [self getUserRelationsCollectionsApi:NO];
    }];
    // 自动改变透明度
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;
    [self.listTableView.mj_header beginRefreshing];
}
#pragma amrk - POST 查询用户的粉丝列表/关注列表
- (void)getUserRelationsCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    
    NSString *url;
    if (self.type == YQPUserFocus) {
        url = kPOST_FOLLOW_LIST_URL;
    }else{
        url = kPOST_FANS_LIST_URL;

    }
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dateArray removeAllObjects];
            }
            self.listModel = [YQPUserAttentionModel mj_objectWithKeyValues:responseObject.data];
            [self.dateArray addObjectsFromArray:self.listModel.list];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        [self stopRefresh];
    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = kAPPTABLEVIEW_BG_COLOR;
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMySetAttentionCell");
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dateArray.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPAttentionModel *model = self.dateArray[indexPath.row];
    MYMySetAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetAttentionCell"];
    cell.nameLb.text = model.name;
    [cell.userIcon sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:IMAGE_NAMED(@"")];
    MJWeakSelf
    [cell.attentionBtn addTapAction:^(id  _Nonnull sender) {
        [weakSelf cancelFocsusApi:model.toUserId];
    }];
    if (self.type == 0) {
        cell.isSelectBtn = true;
    }else{
        cell.isSelectBtn = false;
    }
    
    return cell;
}
#pragma mark - 取消关注
- (void)cancelFocsusApi:(NSString *)toUserId
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_CANCEL_FOLLOW_URL,toUserId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showMessage:kYQLS(@"取消成功")];
        [self getUserRelationsCollectionsApi:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

@end
